/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.appspot.ittrick.drive.trick.task;

import com.appspot.ittrick.drive.trick.api.DriveAPIService;
import com.appspot.ittrick.drive.trick.controller.MainController;
import com.appspot.ittrick.drive.trick.custom.FolderTreeItem;
import com.google.api.services.drive.model.File;
import java.util.ArrayList;
import java.util.List;
import javafx.concurrent.Service;
import javafx.concurrent.Task;

/**
 *
 * @author takahara
 */
public class LaodFolderListTask extends Service<List<FolderTreeItem>> {
    
    private String folderId;
    
    public void run(String folderId) {
        this.folderId = folderId;
        this.restart();
    }

    @Override
    protected Task<List<FolderTreeItem>> createTask() {
        return new Task<List<FolderTreeItem>>() {

            @Override
            protected List<FolderTreeItem> call() throws Exception {
                DriveAPIService driveService = MainController.getInstance().getDriveService();
                
                List<File> childrensList = driveService.getChildrenFoldersList(folderId);

                List<FolderTreeItem> folderTreeItemList = new ArrayList();


                for (File child : childrensList) {
                    FolderTreeItem treeItem = new FolderTreeItem(child);
                    folderTreeItemList.add(treeItem);
                }
                
                return folderTreeItemList;
            }
        };
        
    }
    
}
